package logic

import (
	"context"
	"shoppingcart-srv/shoppingcart"
	"strconv"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddCartLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCartLogic {
	return &AddCartLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddCartLogic) AddCart(req *types.AddCartRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	value := l.ctx.Value("userId").(string)
	userId, _ := strconv.Atoi(value)
	cart, err := l.svcCtx.ShoppingCartSrv.AddCart(l.ctx, &shoppingcart.AddCartRequest{
		GoodsPrice: req.GoodsPrice,
		GoodsId:    req.GoodsId,
		Quantity:   req.Quantity,
		UserId:     int64(userId),
	})
	if err != nil {
		return &types.Response{
			Code:    -1,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	if !cart.Success {
		return &types.Response{
			Code:    -2,
			Message: "添加购物车失败",
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    0,
		Message: "添加购物车成功",
		Data:    nil,
	}, nil
}
